package com.lz.controller;

import com.lly835.bestpay.model.PayResponse;
import com.lz.entity.OrderMaster;
import com.lz.entity.OrderMasterQuery;
import com.lz.enums.ResultEnum;
import com.lz.exception.SellException;
import com.lz.service.OrderService;
import com.lz.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 支付控制器
 * Created by Administrator on 2018/8/29 0029.
 */
@Controller
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private PayService payService;

    @Autowired
    private OrderService orderService;

    @GetMapping("/create")
    public String create(@RequestParam("orderId")String orderId,
                         @RequestParam("returnUrl")String returnUrl,
                         Model model){
        OrderMasterQuery order = orderService.findOne(orderId);
        if (order == null){
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
        }
//        PayResponse payResponse = payService.create(order);
//        model.addAttribute("payResponse",payResponse);
        OrderMaster orderMaster = new OrderMaster();
        orderMaster.setOrderId("123");
        model.addAttribute("orderMaster",orderMaster);
        return "pay/thymeleaf/create";
    }

    @RequestMapping("/notify")
    public String notify(@RequestBody String notifyData){
        PayResponse payResponse = payService.notify(notifyData);

        //返回给微信处理结果
        return "pay/thymeleaf/success";
    }
}
